package com.zhongshengnetwork.rightbe.game.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.customview.PublishSelectPicPopupWindow;
import com.zhongshengnetwork.rightbe.common.dialog.PayDialog;
import com.zhongshengnetwork.rightbe.common.dialog.PayPwdEditText;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.PKMenuModel;
import com.zhongshengnetwork.rightbe.gsonmodel.PKShareModel;
import com.zhongshengnetwork.rightbe.gsonmodel.PKUserModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PKMenuActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener, PayDialog.NetEvevt{
    private final static String url = "http://a.app.qq.com/o/simple.jsp?pkgname=com.zhongshengnetwork.rightbe";//Constant.HOST+"downapp.do?";
    private final static String content = "王者对战，好玩又益智，赶紧来体验吧~";
    private List<String> list;
    private PublishSelectPicPopupWindow menuWindow;

    public static PayDialog.NetEvevt evevt;

    private TopBarView topbar;
    private LinearLayout pk_menu;

    private RelativeLayout first_pk_menu;
    private TextView first_pk_menu_title;
    private TextView first_pk_menu_des;
    private View first_pk_menu_bg;

    private RelativeLayout second_pk_menu;
    private TextView second_pk_menu_title;
    private TextView second_pk_menu_des;
    private View second_pk_menu_bg;

    private RelativeLayout third_pk_menu;
    private TextView third_pk_menu_title;
    private TextView third_pk_menu_des;
    private View third_pk_menu_bg;

    private RelativeLayout fourth_pk_menu;
    private TextView fourth_pk_menu_title;
    private TextView fourth_pk_menu_des;
    private View fourth_pk_menu_bg;

    private RelativeLayout fifth_pk_menu;
    private TextView fifth_pk_menu_title;
    private TextView fifth_pk_menu_des;
    private View fifth_pk_menu_bg;

    private List<PKMenuModel> pkMenuList = new ArrayList<>();
    private PKShareModel pkShareModel;
    private PKUserModel pkUserModel;
    private String code = "";
    private PKMenuModel mPKMenuModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pkmenu);
        evevt = this;
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);
        topbar.getRightTextView().setVisibility(View.VISIBLE);
        topbar.getRightTextView().setText("领金币");
        topbar.getRightTextView().setTextSize(15);
        topbar.getRightTextView().setClickable(true);
        topbar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pkShareModel != null)
                {
                    share(v);
                }

            }
        });

        pk_menu = (LinearLayout)findViewById(R.id.pk_menu);
        pk_menu.setVisibility(View.INVISIBLE);
        first_pk_menu = (RelativeLayout)findViewById(R.id.first_pk_menu);
        first_pk_menu_title = (TextView)findViewById(R.id.first_pk_menu_title);
        first_pk_menu_des = (TextView)findViewById(R.id.first_pk_menu_des);
        first_pk_menu_bg = (View)findViewById(R.id.first_pk_menu_bg);

        second_pk_menu = (RelativeLayout)findViewById(R.id.second_pk_menu);
        second_pk_menu_title = (TextView)findViewById(R.id.second_pk_menu_title);
        second_pk_menu_des = (TextView)findViewById(R.id.second_pk_menu_des);
        second_pk_menu_bg = (View)findViewById(R.id.second_pk_menu_bg);

        third_pk_menu = (RelativeLayout)findViewById(R.id.third_pk_menu);
        third_pk_menu_title = (TextView)findViewById(R.id.third_pk_menu_title);
        third_pk_menu_des = (TextView)findViewById(R.id.third_pk_menu_des);
        third_pk_menu_bg = (View)findViewById(R.id.third_pk_menu_bg);

        fourth_pk_menu = (RelativeLayout)findViewById(R.id.fourth_pk_menu);
        fourth_pk_menu_title = (TextView)findViewById(R.id.fourth_pk_menu_title);
        fourth_pk_menu_des = (TextView)findViewById(R.id.fourth_pk_menu_des);
        fourth_pk_menu_bg = (View)findViewById(R.id.fourth_pk_menu_bg);

        fifth_pk_menu = (RelativeLayout)findViewById(R.id.fifth_pk_menu);
        fifth_pk_menu_title = (TextView)findViewById(R.id.fifth_pk_menu_title);
        fifth_pk_menu_des = (TextView)findViewById(R.id.fifth_pk_menu_des);
        fifth_pk_menu_bg = (View)findViewById(R.id.fifth_pk_menu_bg);

        list = new ArrayList<String>();
        list.add("每日签到");
        list.add("分享给微信好友");
        list.add("分享到微信朋友圈");
        list.add("取消");

        getMenu();
    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LoginActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    public void onClickMenu(View view){
        PKMenuModel pkMenuModel = null;
        switch (view.getId()){
            case R.id.first_pk_menu:
                pkMenuModel = pkMenuList.get(0);
                break;
            case R.id.second_pk_menu:
                pkMenuModel = pkMenuList.get(1);
                break;
            case R.id.third_pk_menu:
                pkMenuModel = pkMenuList.get(2);
                break;
            case R.id.fourth_pk_menu:
                pkMenuModel = pkMenuList.get(3);
                break;
            case R.id.fifth_pk_menu:
                pkMenuModel = pkMenuList.get(4);
                break;
        }
        if (pkMenuModel != null){
            if (pkMenuModel.getType() == 4){
                Intent intent = new Intent(PKMenuActivity.this,GameRangeActivity.class);
                intent.putExtra("appid","pkappid");
                startActivity(intent);
            }else if (pkMenuModel.getType() >= 1 && pkMenuModel.getType() <= 3){
                if (pkMenuModel.getGoldcount() > pkUserModel.getGoldcount()){
                    ToastUtil.show(PKMenuActivity.this,"亲，你金币不足哦");
                    return;
                }
                Intent intent = new Intent(PKMenuActivity.this, PrePKActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("pkuser", pkUserModel);//序列化
                intent.putExtras(bundle);//发送数据
                intent.putExtra("fee",pkMenuModel.getGoldcount());
                intent.putExtra("type",pkMenuModel.getType());
                intent.putExtra("code",CommonUtils.formatString(code));
                startActivity(intent);
                finish();
            }else if (pkMenuModel.getType() == 0){
                mPKMenuModel = pkMenuModel;
                PayDialog payDialog = new PayDialog(PKMenuActivity.this);
                payDialog.show();
            }
        }
    }

    private void getMenu(){
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        String url = "miniapp/"+"pkappid"+"/pkuser.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                Log.e("TAG","数据="+data+"\n");
                pkShareModel = GsonTools.getPKShareModel(data);
                pkUserModel = GsonTools.getPKUserModel(data);
                List<PKMenuModel> list = GsonTools.getPKMenuModel(data);
                if (list != null && list.size() > 0) {
                    pkMenuList.clear();
                    pkMenuList.addAll(list);
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (pkMenuList.size() > 0){
                            pk_menu.setVisibility(View.VISIBLE);
                        }else {
                            pk_menu.setVisibility(View.GONE);
                        }
                        for(int i = 0; i < pkMenuList.size(); i++){
                            PKMenuModel model = pkMenuList.get(i);
                            if (i == 0){
                                first_pk_menu.setVisibility(View.VISIBLE);
                                first_pk_menu_title.setText(model.getName());
                                first_pk_menu_des.setText("");
                                if (model.getType() > 0 && model.getType()< 4){
                                    first_pk_menu_des.setText("金币:"+model.getGoldcount()+" Lv."+model.getLevel());
                                }
                                if (model.isStatus() == true){
                                    first_pk_menu_bg.setVisibility(View.GONE);
                                }else {
                                    first_pk_menu_bg.setVisibility(View.VISIBLE);
                                }
                            }else if (i == 1){
                                second_pk_menu.setVisibility(View.VISIBLE);
                                second_pk_menu_title.setText(model.getName());
                                second_pk_menu_des.setText("");
                                if (model.getType() > 0 && model.getType()< 4){
                                    second_pk_menu_des.setText("金币:"+model.getGoldcount()+" Lv."+model.getLevel());
                                }
                                if (model.isStatus() == true){
                                    second_pk_menu_bg.setVisibility(View.GONE);
                                }else {
                                    second_pk_menu_bg.setVisibility(View.VISIBLE);
                                }
                            }else if (i == 2){
                                third_pk_menu.setVisibility(View.VISIBLE);
                                third_pk_menu_title.setText(model.getName());
                                third_pk_menu_des.setText("");
                                if (model.getType() > 0 && model.getType()< 4){
                                    third_pk_menu_des.setText("金币:"+model.getGoldcount()+" Lv."+model.getLevel());
                                }
                                if (model.isStatus() == true){
                                    third_pk_menu_bg.setVisibility(View.GONE);
                                }else {
                                    third_pk_menu_bg.setVisibility(View.VISIBLE);
                                }
                            }else if (i == 3){
                                fourth_pk_menu.setVisibility(View.VISIBLE);
                                fourth_pk_menu_title.setText(model.getName());
                                fourth_pk_menu_des.setText("");
                                if (model.getType() > 0 && model.getType()< 4){
                                    fourth_pk_menu_des.setText("金币:"+model.getGoldcount()+" Lv."+model.getLevel());
                                }
                                if (model.isStatus() == true){
                                    fourth_pk_menu_bg.setVisibility(View.GONE);
                                }else {
                                    fourth_pk_menu_bg.setVisibility(View.VISIBLE);
                                }
                            }else if (i == 4){
                                fifth_pk_menu.setVisibility(View.VISIBLE);
                                fifth_pk_menu_title.setText(model.getName());
                                fifth_pk_menu_des.setText("");
                                if (model.getType() > 0 && model.getType()< 4){
                                    fifth_pk_menu_des.setText("金币:"+model.getGoldcount()+" Lv."+model.getLevel());
                                }
                                if (model.isStatus() == true){
                                    fifth_pk_menu_bg.setVisibility(View.GONE);
                                }else {
                                    fifth_pk_menu_bg.setVisibility(View.VISIBLE);
                                }
                            }
                        }
                    }
                });
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    private void signDo(){
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        String url = "miniapp/"+"pkappid"+"/usersign.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {// usersign
            @Override
            public void onSuccess(String data) {
                Log.e("TAG",data);
                final CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    ToastUtil.show(PKMenuActivity.this,commonModel.getMsg());
                    getMenu();
                }else {
                    ToastUtil.show(PKMenuActivity.this,"你已领取");
                }
            }
            @Override
            public void onError(String msg) {

            }
        });
    }

    private void share(View v){
        menuWindow = new PublishSelectPicPopupWindow(PKMenuActivity.this,itemsOnClick,list);
        // 显示窗口
        menuWindow.showAtLocation(v,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    // 为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.first_button:
                    signDo();
                    break;
                case R.id.second_button://分享给微信好友
                    shareToWX(1,url,pkShareModel.getName(),content);
                    break;
                case R.id.third_button://分享到微信朋友圈
                    shareToWX(0,url,pkShareModel.getName(),content);
                    break;
                default:
                    break;
            }

        }
    };

    //分享到微信
    private void shareToWX(int type,String url,String title,String content){
        //初始化一个WXWebpageObject对象，填写url
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;
        //用WXWebpageObject对象初始化一个WXMediaMessage对象，填写标题、描述
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = content;
        Bitmap thumb = BitmapFactory.decodeResource(getResources(),R.mipmap.icon);
        msg.thumbData = CommonUtils.bitmapToByteArray(thumb,true);
        //构成一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = msg;
        req.scene = type==0 ? SendMessageToWX.Req.WXSceneTimeline:SendMessageToWX.Req.WXSceneSession;
        CustomApplication.mWxApi.sendReq(req);

    }

    //面对面对战
    @Override
    public void onTextFinish(String text) {
        code = text;
        Intent intent = new Intent(PKMenuActivity.this, PrePKActivity.class);
        Bundle bundle=new Bundle();
        bundle.putSerializable("pkuser", pkUserModel);//序列化
        intent.putExtras(bundle);//发送数据
        intent.putExtra("fee",mPKMenuModel.getGoldcount());
        intent.putExtra("type",mPKMenuModel.getType());
        intent.putExtra("code",CommonUtils.formatString(code));
        startActivity(intent);
        finish();
    }
}
